home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / demo3.zoo / demo / misc / bounce.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-01-24  |  2.7 KB  |  148 lines

  1. /*                        Copyright (c) 1987 Bellcore
  2.  *                            All Rights Reserved
  3.  *       Permission is granted to copy or use this program, EXCEPT that it
  4.  *       may not be sold for profit, the copyright notice must be reproduced
  5.  *       on copies, and credit should be given to Bellcore where it is due.
  6.  *       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7.  */
  8. /*    $Header: bounce.c,v 4.4 88/06/30 15:16:09 sau Exp $
  9.     $Source: /tmp/mgrsrc/demo/misc/RCS/bounce.c,v $
  10. */
  11. static char    RCSid_[] = "$Source: /tmp/mgrsrc/demo/misc/RCS/bounce.c,v $$Revision: 4.4 $";
  12.  
  13. #include <sys/time.h>
  14. #include <stdio.h>
  15. #include "term.h"
  16. #include "restart.h"
  17.  
  18. #define MAXX    999
  19. #define MAXY    999
  20. #define MAXV    60
  21. #define MINV    20
  22. #define LCT    10
  23. #define SLOW    60000        /* usec to sleep between lines */
  24.  
  25. #define fsleep(x) \
  26.    { \
  27.    struct timeval time; \
  28.    time.tv_sec = 0; \
  29.    time.tv_usec = x; \
  30.    select(0,0,0,0,&time); \
  31.    }
  32.  
  33. int vx1, vy1, vx2, vy2;
  34. int x1, y1, x2, y2;
  35. int thex1[LCT];
  36. int they1[LCT];
  37. int thex2[LCT];
  38. int they2[LCT];
  39. int ptr;
  40. int lcolor,bcolor;
  41. long random();
  42.  
  43. main(argc,argv)
  44. char **argv;
  45. {
  46.         register int s = 0;
  47.     int sleep = 0;
  48.  
  49.     ckmgrterm( *argv );
  50.  
  51.     m_setup(0);
  52.     m_push(P_EVENT|P_FLAGS);
  53.     srand(getpid());
  54.     vx1 = 50;
  55.     vy1 = 50;
  56.     x1 = 500;
  57.     y1 = 1;
  58.     vx2 = -50;
  59.     vy2 = -50;
  60.     x2 = 500;
  61.     y2 = MAXY;
  62.  
  63.     if (argc>1 && strcmp(argv[1],"-s")==0)
  64.         sleep++;
  65.  
  66.         Restart();
  67.     m_setevent(UNCOVERED,_quit);
  68.     m_clearmode(M_BACKGROUND);
  69.     for (ptr=0;ptr<LCT;ptr++)
  70.     {
  71.         thex1[ptr] = they1[ptr] = thex2[ptr] = they2[ptr] = -1;
  72.     }
  73.  
  74.     
  75.     bcolor = rand()%24;
  76.     while((lcolor=rand()%24) == bcolor);
  77.     m_bcolor(bcolor);
  78.     m_fcolor(lcolor);
  79.     m_linecolor(B_SRC^B_DST,lcolor);
  80.     m_clear();
  81.     for(;;)
  82.     {
  83.         ptr = (ptr+1) % LCT;
  84.         if (thex1[ptr] >= 0)
  85.             m_line(thex1[ptr],they1[ptr],thex2[ptr],they2[ptr]);
  86.  
  87.         mvpoint(&x1,&y1,&vx1,&vy1);
  88.         mvpoint(&x2,&y2,&vx2,&vy2);
  89.         thex1[ptr] = x1;
  90.         they1[ptr] = y1;
  91.         thex2[ptr] = x2;
  92.         they2[ptr] = y2;
  93.             
  94.         if (thex1[ptr] >= 0)
  95.             m_line(thex1[ptr],they1[ptr],thex2[ptr],they2[ptr]);
  96.         m_flush();
  97.         if (sleep)
  98.            fsleep(90000);
  99.     }
  100. }
  101.  
  102. mvpoint(tx,ty,v_x,v_y)
  103. int *tx,*ty,*v_x,*v_y;
  104. {
  105.  
  106.         *tx += *v_x;    /* move the point */
  107.         *ty += *v_y;
  108.  
  109.         if ( *tx >= MAXX)     /* bounce */
  110.         {
  111.             *v_x = (*v_x > 0) ? -(*v_x) : *v_x;
  112.             diddle(v_x);
  113.         }
  114.         if ( *ty >= MAXY)
  115.         {
  116.             *v_y = (*v_y > 0) ? -(*v_y) : *v_y;
  117.             diddle(v_y);
  118.         }
  119.  
  120.         if ( *tx <= 0)
  121.         {
  122.             *v_x = (*v_x < 0) ? -(*v_x) : *v_x;
  123.             diddle(v_x);
  124.         }
  125.         if ( *ty <= 0)
  126.         {
  127.             *v_y = (*v_y < 0) ? -(*v_y) : *v_y;
  128.             diddle(v_y);
  129.         }
  130. }
  131.  
  132. diddle(ptr)
  133. int *ptr;
  134. {
  135.     int tmp;
  136.     /*
  137.     **    pick a number between MAXV and MINV
  138.     */
  139.     tmp = (rand()% (MAXV-MINV)) + MINV;
  140.     /*
  141.     **     and get the sign right
  142.     */
  143.     if (*ptr < 0)
  144.         *ptr = -tmp;
  145.     else
  146.         *ptr = tmp;
  147. }
  148.